package spring.cloud.tasks.tasks_assign_manager.task_item_assign_event_handler;

import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import spring.cloud.tasks.tasks_assign_manager.service.TaskItemAssignService;
import spring.cloud.tasks.tasks_assign_manager.task_item_assign_event.ExecutorLostOrRecoverTrafficTaskItemAssignEvent;
import spring.cloud.tasks.tasks_assign_manager.task_item_assign_event.TaskItemAssignEvent;

@Slf4j
@Setter
@Getter
public class ExecutorLostOrRecoverTrafficTaskItemAssignEventHandler extends TaskItemAssignEventHandler {
    private TaskItemAssignService taskItemAssignService;

    public ExecutorLostOrRecoverTrafficTaskItemAssignEventHandler(TaskItemAssignService taskItemAssignService) {
        this.taskItemAssignService = taskItemAssignService;
    }

    @Override
    public void handle(TaskItemAssignEvent taskItemAssignEvent) throws Exception {
        try {
            if (taskItemAssignEvent instanceof ExecutorLostOrRecoverTrafficTaskItemAssignEvent) {
                ExecutorLostOrRecoverTrafficTaskItemAssignEvent event = (ExecutorLostOrRecoverTrafficTaskItemAssignEvent) taskItemAssignEvent;
                if (ExecutorLostOrRecoverTrafficTaskItemAssignEvent.type_executor_lost_traffic.equals(event.getType())) {
                    taskItemAssignService.executorLostTrafficAllotTask(event.getExecutorId());
                } else if (ExecutorLostOrRecoverTrafficTaskItemAssignEvent.type_executor_recover_traffic.equals(event.getType())) {
                    taskItemAssignService.executorRecoverTrafficAllotTask(event.getExecutorId());
                }
            }
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
    }


}